<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
<%@ include file="/pages/inc/taglibs.jsp"%>
</head>
<body>
	<div class="container filter">
		<div>
			<input type="button" value="分配" data-target="#addmodal"
				data-toggle="modal" onclick="openExpenseAssign()">
			<div id="expenseAssignList">
				<table class="table table-striped table-bordered" cellspacing="0"
					width="100%">
					<thead>
						<tr>
							<th></th>
							<th>标题</th>
							<th>内容</th>
							<th>费用</th>
							<th>状态</th>
						</tr>
					</thead>
					<tfoot>
						<tr>
							<th></th>
							<th>标题</th>
							<th>内容</th>
							<th>费用</th>
							<th>状态</th>
						</tr>
					</tfoot>
					<tbody>
						<c-rt:forEach items="${expenseAssignList}" var="expenseAssign">
							<tr>
								<td>
								<c-rt:if test="${expenseAssign.status == '未处理'}">
									<input type="checkbox" name="expenseAssignCheckbox"
										value="${expenseAssign.id}">
								</c-rt:if>
								</td>
								<td>${expenseAssign.title}</td>
								<td>${expenseAssign.content}</td>
								<td>${expenseAssign.expense}</td>
								<td>${expenseAssign.status}</td>
							</tr>
						</c-rt:forEach>
	
					</tbody>
				</table>
			</div>
		</div>
		<input type="hidden" id="assignId">
		<div id="addmodal" class="modal fade">
			<div class="modal-dialog modal-lg">
				<div class="container newproject">
					<div class="modal-content">
						<div class="modal-header">
							<h4 class="modal-title">费用分配</h4>
						</div>
						<div class="modal-body">
							<form id="installationForm" class="form-horizontal">
								<table id="expenseAssigeTable">
									<tbody>
										<c-rt:forEach items="${projectList}" var="project">
											<tr>
												<td class="inputLabelTd">${project.projectName}<input
													type="hidden" value="${project.id}"></td>
												<td class="inputTd"><input type="text" class="text">
												</td>
											</tr>
										</c-rt:forEach>
									</tbody>
								</table>
							</form>
						</div>
						<div class="modal-footer">
							<button type="button" class="btn btn-default"
								data-dismiss="modal" onclick="assignExpense()">提交</button>
							<button type="button" class="btn btn-default"
								data-dismiss="modal">取消</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		function openExpenseAssign() {
			if ($('input[name="expenseAssignCheckbox"]:checked').length != 1) {
				alert("只能选择一条记录！");
				return false;
			}
			var id = $('input[name="expenseAssignCheckbox"]:checked').val();
			$("#assignId").val(id);
			$.ajax({
				url : "<c:url value='/expenseAssign/item' />",
				data : {
					assignId : id
				},
				type : 'POST',
				dataType : 'html',
				success : function(data) {
					$("#addmodal").html("");
					$("#addmodal").html(data);
				},
				error : function(data) {
					alert("error");
				}
			});
		}
		function saveAssignExpense() {
			var json = {};
			var info = "";
			var sum = 0;
			$('#expenseAssigeTable').find('tr').each(
					function() {
						info = info
								+ $(this).find('td').eq(0).find('input').val()
								+ ":"
								+ $(this).find('td').eq(1).find('input').val()
								+ ";";
						if ($(this).find('td').eq(1).find('input').val() != "") {
							sum = sum
							+ parseFloat($(this).find('td').eq(1).find(
									'input').val());
						}	
					});
			if (sum > 100) {
				alert("项目分配费用百分比和不能超过100%");
				return;
			} else {
				json.assignId = $("#assignId").val();
				json.info = info;
				$.ajax({
					url : "<c:url value='/expenseAssign/save'/>",
					type : 'POST',
					dataType : 'json',
					data : json,
					success : function(data) {
						$('#installationForm')[0].reset();
					},
					error : function(data) {
						alert("error");
					}
				});
			}

		}
		
		function commitAssignExpense() {
			var json = {};
			json.assignId = $("#assignId").val();
			$.ajax({
				url : "<c:url value='/projectExpense/commit' />",
				type : 'POST',
				dataType : 'json',
				data : json,
				success : function(data) {
					query();
				},
				error : function(data) {
					alert("error");
				}
			});
		}
		
		function query() {
			$.ajax({
				url : "<c:url value='/expenseAssign/list' />",
				data : {},
				type : 'POST',
				dataType : 'html',
				success : function(data) {
					$("#expenseAssignList").html("");
					$("#expenseAssignList").html(data);
				},
				error : function(data) {
					alert("error");
				}
			});
		}
	</script>
</body>
</html>